# 检查用户名
import re

# username = input("input your name: ")
username = ['fa_', '34adf_', '_afdf22', 'fad342ad21213121sdfsdsd', 'fad342ad_']


def ree(x):
    return re.findall(r"^[a-zA-Z][\w ]{5,17}$", x)


print(list(filter(lambda item: len(item) != 0, map(ree, username))))
print()
# [['fad342ad_']]

# ---------------------------------------------------------------------------
# 匿名函数+reduce实现n的阶乘
from functools import reduce

print(reduce(lambda x, y: x * y, range(1, 3)))
print(reduce(lambda x, y: x * y, range(1, 4)))
print(reduce(lambda x, y: x * y, range(1, 5)))
print()
# 2
# 6
# 24

# ---------------------------------------------------------------------------
# filter函数：找出[1-100]能被3整除的数
print(list(filter(lambda item: item % 3 == 0, range(1, 101))))
print()
# [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]


# ---------------------------------------------------------------------------
# map函数:求list[1-10]各项的平方
print(list(map(lambda item: item ** 2, range(1, 11))))
print()
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

# ---------------------------------------------------------------------------
# 将列表[1, 4, 5, 9]转换成'1459'字符串
a = [1, 4, 5, 9, 1]
b = {}
for i in a:
    b[i] = str(i)
print(b)
print(reduce(lambda x, y: x + y, list(map(lambda item: b[item], a))))
print(reduce(lambda x, y: str(x) + str(y), a))
print()
# {1: '1', 4: '4', 5: '5', 9: '9'}
# 14591
# 14591

# ---------------------------------------------------------------------------
# 列表正负整数排序
list1 = [0, 7, -8, 5, 4, -2, -5]

# 返回元组进行排序
# 这里我们知道了在匿名函数中也可以使用比较复杂的if条件返回函数
print(sorted(list1, key=lambda x: abs(x) + 10 if x <= 0 else x))
# 这里我们知道在匿名函数中也可以使用元组进行排序，先排元组的第一位，如果一样，再排第二位
print(sorted(list1, key=lambda x: (x <= 0, abs(x))))
print(sorted(list1, key=lambda x: max(list1) - x + 1 if x <= 0 else x))
print()
# [0, 4, 5, 7, -2, -5, -8]

# ---------------------------------------------------------------------------
# 字符串排序
s = "asbf234GDSdsf23"


def func1(x):
    if 48 <= ord(x) <= 57:
        if ord(x) % 2 == 0:
            return ord(x)
        else:
            return ord(x) - 10
    elif 97 <= ord(x) <= 122:
        return ord(x) - 100
    else:
        return ord(x) - 52


print(sorted(s, key=func1))
print(list(map(func1, sorted(s, key=func1))))
# ['a', 'b', 'd', 'f', 'f', 's', 's', 'D', 'G', 'S', '3', '3', '2', '2', '4']
# [-3, -2, 0, 2, 2, 15, 15, 16, 19, 31, 41, 41, 50, 50, 52]
# a false,false,a
# A false,true,A
# 奇数 true,false,ji
# 偶数 true,true,ou
print(sorted(s, key=lambda x: (x.isdigit(), x.isdigit() and int(x) % 2 == 0, x.isupper(), x)))
# 偶数 《 奇数 《 小写 《 大写
print(sorted(s, key=lambda x: (x.isdigit(), x.isdigit() and int(x) % 2 == 0, x.islower(), x), reverse=True))